import sys
from collections import Counter
def get_ints(): return list(map(int, sys.stdin.readline().strip().split()))
sys.setrecursionlimit(20000)
T = int(input())
for _ in range(T):
n = get_ints()[0]
s = sys.stdin.readline()
count = Counter()
for ss in s:
count[ss]+=1
ans = -1
for x in count:
l = 0
r = (n - 1)
removals = 0
while l < r:
if s[l] == s[r]:
l+= 1
r-= 1
elif s[l] == x:
l+= 1
removals+= 1
elif s[r] == x:
r-= 1
removals+= 1
else:
removals = -1
break
if removals != -1:
if removals <= ans or ans == -1:
ans = removals
print(ans)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define fr(n) for(ll i=0;i<n;i++)
#define frj(m) for(ll j=0;j<m;j++)
#define fro(m) for(ll i=1; i<=m; i++)
#define frr(n) for(ll i=n;i>=0;i--)
#define nl cout<<"\n"
#define nll '\n'
#define pr(n) cout<<n<<'\n';
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define inf 1ll<<62
#define inff 1<<30
#define debug(arr,n) for(int i=0; i<n; i++) cout<<arr[i]<<" "; cout<<"\n"
#define debugr(arr,s,e) for(int i=s; i<=e; i++) cout<<arr[i]<<" "; cout<<"\n"
#define inp freopen("input.txt", "r", stdin)
#define outp freopen("output.txt", "w", stdout)
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
int main(){
fastio;
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
string s;
cin>>s;
ll ans=-1;
for(char ch='a';ch<='z';ch++){
ll l=0,r=n-1,cnt=0,f=0;
while(l<r){
if(s[l]==s[r]){
l++,r--;
}
else{
if(s[l]==ch){
l++;cnt++;
}
else if(s[r]==ch){
r--;cnt++;
}
else{
f=1;break;
}
}
}
if(!f){
if(ans==-1) ans=cnt;
else ans=min(ans,cnt);
}
}
cout<<ans<<nll;
}
return 0;
}
499B - Lecture | 709A - Juicer |
1358C - Celex Update | 1466B - Last minute enhancements |
450B - Jzzhu and Sequences | 1582C - Grandma Capa Knits a Scarf |
492A - Vanya and Cubes | 217A - Ice Skating |
270A - Fancy Fence | 181A - Series of Crimes |
1638A - Reverse | 1654C - Alice and the Cake |
369A - Valera and Plates | 1626A - Equidistant Letters |
977D - Divide by three multiply by two | 1654B - Prefix Removals |
1654A - Maximum Cake Tastiness | 1649A - Game |
139A - Petr and Book | 1612A - Distance |
520A - Pangram | 124A - The number of positions |
1041A - Heist | 901A - Hashing Trees |
1283A - Minutes Before the New Year | 1654D - Potion Brewing Class |
1107B - Digital root | 25A - IQ test |
785A - Anton and Polyhedrons | 1542B - Plus and Multiply |